<?php

class Welcome extends CI_Controller {

    // 自动装载模型
    public function __construct() {
        parent::__construct();
        // $this->output->enable_profiler(TRUE);
        // $this->load->model('Stu_model', 'stu');
        $this->load->model('Taskinfo_model', 'task');
    }

    public function index() {
        // $this->load->view('welcome_message');
        // $this->load->helper('url');
        echo base_url();
        $this->load->view('form.html');
    }

    public function handle() {
        // echo '我将对接收到的数据进行处理！';

        // 载入验证类
        $this->load->library('form_validation');

        // 设置验证规则
        // 验证规则的三个参数分别是字段名、提示信息、验证规则
        $this->form_validation->set_rules('stuname', '姓名', 'callback_username_check');
        $this->form_validation->set_rules('stunumber', '学号', 'callback_stunumber_check');

        // 运行验证规则
        if ($this->form_validation->run()) {
            // 如果通过验证，则将其保存到数据库
            $this->saveToDb();
            // 获取全部数据
            $data['students'] = $this->task->getAll();
            // 调用视图，显示用户信息及提示信息
            // echo "<pre>";
            // var_dump($data['students']);exit();
            // echo "</pre>";
            $this->load->view('message.html', $data);
        } else {
            $this->load->view('form.html');
        }
    }

    public function username_check($str) {
        $pattern = '/^[\x80-\xff]{6,}$/';
        if (!preg_match($pattern, $str)) {
            $this->form_validation->set_message('username_check', '姓名至少为两个汉字，不能有数字和字母');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function stunumber_check($str) {
        $pattern = '/^[pP]1[3-7]\d{7}$/';
        if (!preg_match($pattern, $str)) {
            $this->form_validation->set_message('stunumber_check', '学号必须为10位，且开头为p');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function re($value = '') {
        $pattern = '/^[\x80-\xff]{6,}$/';
        // $pattern = '/[1-4a-h]/';
        $string = '中';
        $this->outputRe($pattern, $string);
    }

    public function outputRe($pattern, $string) {

        if (preg_match_all($pattern, $string, $matched)) {
            echo '正则表达式:', htmlentities($pattern), '和字符串：', htmlentities($string), '匹配成功，匹配的字符串是：', "<br />";
            echo '<pre>';
            var_dump($matched);
            echo '</pre>';
        } else {
            echo "<p>", '没找到匹配字符', "</p>";
        }
    }

    /**
     * 存储表单数据到数据库
     */
    public function saveToDb() {
        // 先将数据存放到数组
        $data['stuname']   = $this->input->post('stuname');
        $data['stunumber'] = $this->input->post('stunumber');
        $data['stuclass']  = $this->input->post('stuclass');
        $data['stuurl']    = $this->input->post('stuurl');
        // 调用模型存储数据
        $this->task->add($data);
    }

    public function test() {
        $this->load->view('message.html');
    }
}